package org.javaforever.cookieshop.controller;

import java.util.List;

import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.javaforever.cookieshop.domain.GoodsBean;
import org.javaforever.cookieshop.domain.PageBean;
import org.javaforever.cookieshop.domain.TypeBean;
import org.javaforever.cookieshop.service.CustOrderService;
import org.javaforever.cookieshop.service.GoodsService;
import org.javaforever.cookieshop.service.TypeService;
import org.javaforever.cookieshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class AdminGoodsCustController { 
	@Autowired
	protected GoodsService goodsService;
	@Autowired
	protected CustOrderService orderService;
	@Autowired
	protected TypeService typeService;
	@Autowired
	protected UserService userService;

    @GetMapping("/admin/goods_list")
    public String adminGoodsList(@RequestParam(value = "type", defaultValue = "0") Long type, @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber, Model model) throws Exception{   
	    if(pageNumber<=0) pageNumber=1;
	    PageBean p = goodsService.getGoodsRecommendPage(type, pageNumber);
	    if(p.getTotalPage()==0)
	    {
	        p.setTotalPage(1);
	        p.setPageNumber(1);
	    }
	    else {
	        if(pageNumber>=p.getTotalPage()+1)
	        {
	            p = goodsService.getGoodsRecommendPage(type, pageNumber);
	        }
	    }
	    model.addAttribute("p", p);
	    model.addAttribute("type", type);
	    return "/admin/goods_list";
    }

    @GetMapping("/admin/go_goods_add")
    public String adminGoGoodsAdd(Model model) throws Exception{   
	    try {
	        List<TypeBean> list = typeService.listActiveTypes();

	        model.addAttribute("typeList",list);
	        return "/admin/goods_add";
	    } catch (FileUploadException e) {
	        e.printStackTrace();
	        return "/admin/goods_add";
	    }
	}
    
    @PostMapping("/admin/goods_add")
    public String adminGoodsAdd(@RequestBody GoodsBean goods, Model model) throws Exception{   
	    try {
	        GoodsBean g = new GoodsBean();

	        goodsService.addGoods(g);
	        return "/admin/goods_list";
	    } catch (FileUploadException e) {
	        e.printStackTrace();
	        return "/admin/goods_list";
	    }
	}
    
    @GetMapping("/admin/goods_editshow")
    public String adminGoodsEditShow(@RequestParam(value = "id", required = true) Long id, Model model) throws Exception{   
        GoodsBean g = goodsService.findGoodsById(id);
        List<TypeBean> typeList = typeService.listActiveTypes();
        model.addAttribute("g", g);
        model.addAttribute("typeList", typeList);
        return "/admin/goods_edit";
	}
    
    @GetMapping("/admin/goods_delete")
    public String adminGoodsDelete(@RequestParam(value = "id", required = true) Long id, Model model) throws Exception{   
	    try {
	        goodsService.deleteGoods(id);
	        return "redirect:/admin/goods_list";
	    } catch (FileUploadException e) {
	        e.printStackTrace();
	        return "redirect:/admin/goods_list";
	    }
	}
    
    @GetMapping("/admin/goods_softdelete")
    public String adminGoodsSoftDelete(@RequestParam(value = "id", required = true) Long id, Model model) throws Exception{   
	    try {
	        goodsService.softDeleteGoods(id);
	        return "redirect:/admin/goods_list";
	    } catch (FileUploadException e) {
	        e.printStackTrace();
	        return "redirect:/admin/goods_list";
	    }
	}
    
    @GetMapping("/admin/goods_recommend")
    public String adminGoodsRecommend(@RequestParam(value = "id", required = true) Long id,@RequestParam(value = "method", required = true) String method,@RequestParam(value = "typeTarget", required = true) Integer typeTarget,Model model) throws Exception{   
        if(method.equals("add")) {
            goodsService.addRecommend(id, typeTarget);
        }else {
            goodsService.removeRecommend(id, typeTarget);
        }
        return "redirect:/admin/goods_list";
	}    
}
